import request from '../../utils/request';
import {DynamicRoutes, configRouter} from '../../routerConfig';
import router from '../../router';
const user = {
  state: {
    routeList: null,
    menu: null,
  },

  getters: {

  },

  mutations: {
    SET_ROUTERlIST(state, list) {
      state.routeList = list;
    },
    SET_MENU(state, menu) {
      state.menu = menu;
    }
  },

  actions: {
    // 获取权限列表
    // /permission/getCfgPermissionList.htm
    // /permission/list.htm
    GET_PERMISSION_LIST({ commit }) {
      return new Promise((resolve, reject) => {
        request.get('/permission/list.htm').then(res => {
          console.log('res::', res);
          console.log('commit::', commit);
          resolve(res);
        }).catch(err => {
          console.error('err::::', err);
          reject(err);
        })
      });
    },

    /**
     * 获取权限配置列表
     * @params permissionId string 部门id
    */
    GET_PERMISSION_CONFIG_LIST({ commit }, payload) {
      return new Promise((resolve, reject) => {
        request.post('/permission/getCfgPermissionList.htm', payload).then(res => {
          console.log('commit::', commit);
          resolve(res);
        }).catch(err => {
          console.error('err::::', err);
          reject(err);
        })
      });
    },

    /**
     * 获取用户所在部门权限列表
     * @params permissionId string 部门id
    */
    async GET_USER_PERMISSION({ commit }, payload) {
      // let data = await request.post('/permission/getCfgPermissionList.htm', payload);
      // let list = [];
      // let err = null;
      // if(data.data.retCode == 0) {
      //   list = data.data.data.purviewList;
      // }
      return new Promise((resolve, reject) => {
        request.post('/permission/getCfgPermissionList.htm', payload).then(res => {
          console.log('commit::', commit);
          if(res.data.retCode == 0) {
            const list = res.data.data.purviewList;
            const menu = [];
            for(let i = 0; i < list.length; i++) {
              if(list[i].own == 1) {
                menu.push(list[i]);
                if(configRouter.hasOwnProperty(list[i].purview)) {
                  DynamicRoutes.children.push.apply(DynamicRoutes.children, configRouter[list[i].purview]);
                }
              }
            }
            console.log('menu::', menu);
            commit('SET_MENU', menu);
            
            const DynamicRoutesList = [];
            DynamicRoutesList.push(DynamicRoutes);
            DynamicRoutesList.push(configRouter.notFound);
            commit('SET_ROUTERlIST', DynamicRoutesList);
            router.addRoutes(DynamicRoutesList);
            console.log('router:::', router);
            console.log('DynamicRoutesList::', DynamicRoutesList);
            resolve(DynamicRoutesList);
          }
          
        }).catch(err => {
          console.error('err::::', err);
          reject(err);
        })
      });
    },

    /**
     * 权限管理-添加/修改/删除部门权限
     * @params payload {
     *  name string
     *  description string 部门描述
     *  state string  	状态 normal("正常/启用") deleted("已删除"); expired("已失效"),
     *  subPurviews string 二级视图权限，权限id,权限id2 如 1,3,4,8
     *  id string 部门id ，为null新增，否则修改
     * }
     * */
    UPDATE_DEPARTMENT({ }, payload) {
      return new Promise((resolve, reject) => {
        request.post('/permission/save.htm', payload).then(res => {
          resolve(res);
        }).catch(err => {
          console.error('err::::', err);
          reject(err);
        })
      });
    }

  }
}

export default user